package com.question.business.post.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.question.business.post.model.ao.*;
import com.question.business.post.model.vo.PostAddVO;
import com.question.business.post.model.vo.PostDetailVO;
import com.question.business.post.model.vo.PostDropDownVO;
import com.question.business.post.model.vo.PostVO;
import com.question.business.post.service.IPostService;
import com.question.common.annotation.DataConvert;
import com.question.common.utils.DateUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;


/**
 * 岗位接口
 *
 * @author star
 * @since 2023-09-13
 */
@RestController
@AllArgsConstructor
@RequestMapping("/post")
public class PostController {

    private final IPostService service;


    /**
     * 岗位分页查询
     *
     * @param ao 请求参数
     * @return Page<PostVO> 响应参数
     */
    @GetMapping("/page")
    public Page<PostVO> queryPostPage(@Validated PostPageAO ao) {
        return service.queryPostPage(ao);
    }

    /**
     * 岗位明细查询
     *
     * @param id 岗位ID
     * @return PostDetailVO 响应参数
     */
    @DataConvert
    @GetMapping("/detail")
    public PostDetailVO queryPostDetail(@Validated @NotBlank(message = "删除数据不能为空") @Size(max = 20, message = "ID最大长度不能超过20") String id) {
        return service.queryPostDetail(id);
    }

    /**
     * 岗位新增数据
     *
     * @param ao 请求参数
     * @return PostAddVO 响应参数
     */
    @PostMapping("/add")
    public PostAddVO addPost(@Validated @RequestBody PostAddAO ao) {
        return service.addPost(ao);
    }

    /**
     * 岗位修改数据
     *
     * @param ao 数据
     */
    @PutMapping("/update")
    public void updatePost(@Validated @RequestBody PostUpdateAO ao) {
        service.updatePost(ao);
    }

    /**
     * 岗位删除数据
     *
     * @param id 岗位ID
     */
    @DeleteMapping("/delete")
    public void deletePost(@Validated @NotBlank(message = "删除数据不能为空") @Size(max = 20, message = "ID最大长度不能超过20") String id) {
        service.deletePost(id);
    }

    /**
     * 岗位批量删除数据
     *
     * @param ids 请求参数
     */
    @DeleteMapping("/batch/delete")
    public void deleteBatchPost(@RequestParam @Validated @NotNull(message = "删除数据不能为空") @Size(min = 1, message = "删除数据不能为空") List<@NotBlank(message = "删除数据不能为空") @Size(max = 20, message = "ID最大长度不能超过20") String> ids) {
        service.deleteBatchPost(ids);
    }

    /**
     * 岗位导出数据
     *
     * @param ao       请求参数
     * @param response 响应流
     */
    @GetMapping("/export")
    public void exportPost(PostPageAO ao, HttpServletResponse response) throws IOException {
        response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("岗位-" + DateUtil.getStringTime() + ".xlsx", "UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        service.exportPost(ao, outputStream);
        IOUtils.closeQuietly(outputStream);
    }

    /**
     * 岗位启用、停用
     *
     * @param ao 请求参数
     */
    @PutMapping("/enable")
    public void enablePost(@Validated @RequestBody PostEnabledAO ao) {
        service.enablePost(ao);
    }

    /**
     * 岗位下拉列表查询
     *
     * @param ao 请求参数
     * @return List<PostDropDownVO> 响应参数
     */
    @GetMapping("/drop/down")
    public List<PostDropDownVO> queryPostDropDown(@Validated PostDropDownAO ao) {
        return service.queryPostDropDown(ao);
    }

}
